﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gift.Utile;
using Gift.Model;
using Gift.WebApp;
using System.Web.Security;
using System.Data;

namespace WebSite.admin.company
{
    public partial class companyManager : BaseAdmin
    {

     
        public int ParaCompanyId
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    return 0;
                }
                else
                {
                    return int.Parse(Request.QueryString["id"]);
                }
            }
        }
        public string Action
        {
            get
            {
                return Request.QueryString["action"];

            }
        }




        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                    if (Request.QueryString["ot"] == "ed")
                    {
                        base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyEdit);
                    }
                    else
                    {
                        base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyAdd);
                    }


                    if (Request.QueryString["ot"] == "ed")
                {
                    this.txtName.Enabled = false;
                    this.txtShortName.Enabled = false;
                    this.txtCompanyCode.Enabled = false;
                    this.txtManager.Enabled = false;
                    this.txtManagerMobile.Enabled = false;
                    //this.AddressArea1.Visible = false;
                    this.txtAddress.Enabled = false;
                    this.ddlUpCompany.Enabled = false;
                    this.ddlCompanyLevel.Enabled = false;
                    tbCompany oCompany = BaseFactory.theCompany.GetModel(ParaCompanyId);
                    if (oCompany.ParentId.ToString() == base.Administrator.CompanyId)
                    {
                        this.txtOnSale.Enabled = true;
                        this.txtCreditMoney.Enabled = true;
                    }
                    else
                    {
                        this.txtOnSale.Enabled = false;
                        this.txtCreditMoney.Enabled = false;
                    }

                }
                this.BindCompanyLevel();
                this.BindPrivence();
                this.BindCity();
                this.BindArea();
                this.BuildForm();
            }
        }

        void BuildForm()
        {
            if (Action == "del")
            {
                if (base.Administrator.CompanyId != null && base.Administrator.CompanyId == ParaCompanyId.ToString())
                {
                    ExtAspNet.Alert.Show("不能删除自己！");
                    base.Response.End();
                }
                else
                {
                    if (BaseFactory.theCompany.ExistsDownCompany(ParaCompanyId))
                    {
                        ExtAspNet.Alert.Show("您有下级商家，如要删除，请先删除下级商家！");
                    
                        base.Response.End();
                    }
                    else
                    {
                        BaseFactory.theCompany.Delete(ParaCompanyId);
                        ExtAspNet.Alert.Show("删除成功！","","window.location.href='companyList.aspx';");
  
                        base.Response.End();
                    }
                }
            }

            else if (Request.QueryString["ot"] == "ed" )
            {
                tbCompany oCompany;
                oCompany = BaseFactory.theCompany.GetModel(ParaCompanyId);
            
                    this.txtAddress.Text = oCompany.Address;
                   
                    this.txtEmail.Text = oCompany.Email;
                    this.txtFax.Text = oCompany.Fax;
                    this.txtLinker.Text = oCompany.Linker;
                    this.txtName.Text = oCompany.Name;
                    this.hdCompany.Value = oCompany.Name;
                    this.txtPostcode.Text = oCompany.Postcode;
                    this.txtQQ.Text = oCompany.QQ;
                    this.txtTel.Text = oCompany.Telephone;
                    if (oCompany.LevelId == 0)
                    {
                        this.ddlCompanyLevel.Items.Insert(0, new ExtAspNet.ListItem("顶级", "0"));
                    }
                    this.ddlCompanyLevel.Items.FindByValue(oCompany.LevelId.ToString()).Selected = true;

                    this.txtOnSale.Text = BaseFactory.theCompany.GetCompanyOnSale(oCompany.Id.ToString()).ToString();
                    this.txtCreditMoney.Text = oCompany.CreditMoney.ToString();
                    this.txtMobile.Text = oCompany.Mobile;
                    this.txtCompanyCode.Text = oCompany.CompanyCode;
                    this.txtShortName.Text = oCompany.CompanyShortName;
                    this.txtManager.Text = oCompany.CompanyManager;
                    this.txtManagerMobile.Text = oCompany.CompanyManagerMobile;
                
                    if (this.ddlCompanyCategory.SelectedItem != null)
                    {
                        this.ddlCompanyCategory.SelectedItem.Selected = false;
                    }
                    this.ddlCompanyCategory.Items.FindByValue(oCompany.CompanyCategoryId.ToString()).Selected = true;


                    if (oCompany.ParentId.ToString() == "0")
                    {
                        this.ddlUpCompany.Items.Insert(0, new ExtAspNet.ListItem("顶级", "0"));
                    }
                    else
                    {
                        tbCompany oUpCompany = BaseFactory.theCompany.GetModel(oCompany.ParentId);
                        this.ddlUpCompany.Items.Insert(0, new ExtAspNet.ListItem(oUpCompany.Name.ToString(), oUpCompany.Id.ToString()));
                    }


                    if (base.Administrator.CompanyId == ParaCompanyId.ToString())
                    {
                        this.ddlCompanyLevel.Enabled = false;
                        this.txtOnSale.Enabled = false;
                        this.txtUserName.Text = base.Administrator.AdminUser;
                        this.txtPassword.Enabled = true;

                    }
                    else
                    {

                        this.txtPassword.Enabled = false;
                        this.txtUserName.Hidden = true;
                        this.txtPassword.Hidden = true;
                    }
            
                    if (this.ddlSheng.Items.FindByText(oCompany.Provence) != null)
                    {
                        this.ddlSheng.Items.FindByText(oCompany.Provence).Selected = true;
                        this.ddlSheng.DataBind();
                        this.BindCity();
                    }
                    if (this.ddlShi.Items.FindByText(oCompany.City) != null)
                    {
                        this.ddlShi.Items.FindByText(oCompany.City).Selected = true;
                        this.ddlShi.DataBind();
                        this.BindArea();
                    }
                    if (this.ddlXian.Items.FindByText(oCompany.Area) != null)
                    {
                        this.ddlXian.Items.FindByText(oCompany.Area).Selected = true;
                        this.ddlXian.DataBind();
                    }
                    this.ddlSheng.Enabled = false;
                    this.ddlShi.Enabled = false;
                    this.ddlXian.Enabled = false;

                    this.txtUserName.Enabled = false;
                    this.ddlUpCompany.Enabled = false;
                }
            else
            {
                this.txtCompanyCode.Text = BaseFactory.theCompany.GetGenerateCompanyCode().ToString();

                if (Request.QueryString["ot"] == "ad")
                {
                    tbCompany oUpCompany = BaseFactory.theCompany.GetModel(int.Parse(base.Administrator.CompanyId));
                   this.ddlUpCompany.Items.Insert(0, new ExtAspNet.ListItem(oUpCompany.Name.ToString(), oUpCompany.Id.ToString()));
                   
                }
            }
            
   
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            tbCompany oCompany;
            tbAdminLogin oAdminLogin;
            oCompany = null;
            if (ParaCompanyId > 0)
            {
                oCompany = BaseFactory.theCompany.GetModel(ParaCompanyId);
            }

            if (oCompany == null) oCompany = new tbCompany();
            tbCompany oUpCompany = BaseFactory.theCompany.GetModel(int.Parse(this.ddlUpCompany.SelectedItem.Value));

            try
            {
                double dSale = double.Parse(this.txtOnSale.Text.Trim());
                if (dSale < 0.5 || dSale > 1.0)
                {
                    ExtAspNet.Alert.Show("折扣只能为0.1-1.00的数字！");
                    return;
                }

            }
            catch
            {
                ExtAspNet.Alert.Show("折扣只能为0.1-1.00的数字！");
                return;
            }

            try
            {
                double dCreditMoney = double.Parse(this.txtCreditMoney.Text.Trim());
                if (dCreditMoney < 0)
                {
                    ExtAspNet.Alert.Show("授信额度只能为大于等于0的数字！");
                   
                    return;
                }
            }
            catch
            {
                ExtAspNet.Alert.Show("授信额度只能为大于等于0的数字！");
             
                return;
            }


            oCompany.Id = ParaCompanyId;

            oCompany.Email = this.txtEmail.Text.Trim();
            oCompany.Linker = this.txtLinker.Text.Trim();
            oCompany.Fax = this.txtFax.Text.Trim();
            oCompany.Name = this.txtName.Text.Trim();
            oCompany.Postcode = this.txtPostcode.Text.Trim();
            oCompany.QQ = this.txtQQ.Text.Trim();
            oCompany.Telephone = this.txtTel.Text.Trim();
     

             oCompany.ParentId = this.ddlUpCompany.SelectedItem.Value == "" ? 0 : int.Parse(this.ddlUpCompany.SelectedItem.Value);
     
            oCompany.LevelId = int.Parse(this.ddlCompanyLevel.SelectedItem.Value);
            oCompany.Mobile = this.txtMobile.Text.Trim();
            oCompany.CompanyCode = this.txtCompanyCode.Text.Trim();
            oCompany.CompanyShortName = this.txtShortName.Text.Trim();
            oCompany.CompanyManager = this.txtManager.Text.Trim();
            oCompany.CompanyManagerMobile = this.txtManagerMobile.Text.Trim();
            oCompany.CreditMoney = this.txtCreditMoney.Text.Trim() == "" ? 0 : decimal.Parse(this.txtCreditMoney.Text);
            oCompany.Provence = this.ddlSheng.SelectedItem.Text;
            oCompany.City = this.ddlShi.SelectedItem.Text;
            if (this.ddlXian.SelectedItem.Value != "")
            {
                oCompany.Area = this.ddlXian.SelectedItem.Text;
            }
            else
            {
                oCompany.Area = "";
            }
            oCompany.Address = this.txtAddress.Text.Trim();
            try
            {
                if (ParaCompanyId > 0)
                {
     
                    if (BaseFactory.theCompany.ExistsByCode(this.txtCompanyCode.Text.Trim()) && oCompany.CompanyCode != this.txtCompanyCode.Text.Trim())
                    {
                        ExtAspNet.Alert.Show("系统中已有此公司编码，请重新设置编码！");

                        this.txtCompanyCode.Text = BaseFactory.theCompany.GetGenerateCompanyCode().ToString();
                        return;
                    }

                    if (this.hdCompany.Value != oCompany.Name)
                    {
                        tbCompany tempCompany = BaseFactory.theCompany.GetModelByName(oCompany.Name);
                        if (tempCompany != null)
                        {
                            ExtAspNet.Alert.Show("该公司名已存在,请重新输入公司名！");
                            return;
                        }
                    }
                    if (oCompany.CompanyShortName != this.txtShortName.Text.Trim()
                         && BaseFactory.theCompany.ExistsByShortName(this.txtShortName.Text.Trim()))
                    {
                        ExtAspNet.Alert.Show("该公司简称已存在,请重新输入公司简称！");
                        return;
                    }

                    if (oUpCompany.LevelId >= int.Parse(this.ddlCompanyLevel.SelectedItem.Value) && oCompany.LevelId != 1 && oCompany.LevelId != 0)
                    {
                        ExtAspNet.Alert.Show("上级公司级别不能小于添加的公司级别！");
                        return;
                    }

                    BaseFactory.theCompany.Update(oCompany);

                    BaseFactory.theCompany.IsExistsOnSale(oCompany.Id.ToString(), float.Parse(this.txtOnSale.Text.Trim()));

                }
                else
                {
      
                    oCompany.CompanyCategoryId = int.Parse(this.ddlCompanyCategory.SelectedItem.Value);

                    tbCompany tempCompany = BaseFactory.theCompany.GetModelByName(oCompany.Name);
                    if (tempCompany != null)
                    {
                        ExtAspNet.Alert.Show("该公司名已存在,请重新输入公司名！");
                        return;
                    }

                    if (BaseFactory.theCompany.ExistsByShortName(this.txtShortName.Text.Trim()))
                    {
                        ExtAspNet.Alert.Show("该公司简称已存在,请重新输入公司简称！");

                        return;
                    }

                    if (BaseFactory.theCompany.ExistsByCode(this.txtCompanyCode.Text.Trim()))
                    {
                        ExtAspNet.Alert.Show("系统中已有此公司编码，请重新设置编码！");
                        this.txtCompanyCode.Text = BaseFactory.theCompany.GetGenerateCompanyCode().ToString();
                        return;
                    }
                    oAdminLogin = new tbAdminLogin();

                    int iAdminId = BaseFactory.theAdmin.GetIdByName(this.txtUserName.Text.Trim());
                    if (iAdminId > 0)
                    {
                        ExtAspNet.Alert.Show("该用户名已存在,请重新输入用户名！");
                        return;
                    }

                    if (oUpCompany.LevelId >= int.Parse(this.ddlCompanyLevel.SelectedItem.Value) && oCompany.LevelId != 1)
                    {
                        ExtAspNet.Alert.Show("上级公司级别不能小于添加的公司级别！");
                        return;
                    }

                    //if (this.txtPassword.Text.Trim() == "" || this.txtConfirmPass.Text.Trim() == "")
                    //{
                    //    base.Response.Write("<script language='javascript'>alert('密码不能为空！');</script>");
                    //    return;
                    //}
                    oAdminLogin.AdminUser = this.txtUserName.Text.Trim();
                    oAdminLogin.AdminPwd = re.EncodeMD5(this.txtPassword.Text.Trim());
                    iAdminId = BaseFactory.theAdmin.Add(oAdminLogin);

                    oCompany.AdminId = iAdminId;
                    BaseFactory.theCompany.Add(oCompany);

               
                }

                ExtAspNet.Alert.Show("保存成功！","","window.location.href='companyList.aspx';");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }


        void BindCompanyLevel()
        {
            DataSet ds = BaseFactory.theCompany.GetListForCompanyLevel(base.Administrator.CompanyId);
            this.ddlCompanyLevel.DataTextField = "LevelName";
            this.ddlCompanyLevel.DataValueField = "Id";
            if (ds != null && ds.Tables.Count > 0)
            {
                this.ddlCompanyLevel.DataSource = ds.Tables[0].DefaultView;
                this.ddlCompanyLevel.DataBind();
            }
        }


        private void BindPrivence()
        {
            this.ddlSheng.Items.Clear();
            DataTable dtPrivence = BaseFactory.theRegion.GetRegionByParentRegionAndCompanyId(0,int.Parse(base.Administrator.CompanyId));
            ddlSheng.DataTextField = "regionName";
            ddlSheng.DataValueField = "id";
            ddlSheng.DataSource = dtPrivence;
            ddlSheng.DataBind();

            ddlSheng.Items.Insert(0, new ExtAspNet.ListItem("选择省份", "-1"));
        }

        private void BindCity()
        {
            string sheng = ddlSheng.SelectedValue;
            this.ddlShi.Items.Clear();
            if (sheng != "-1")
            {
                DataTable dtCity = BaseFactory.theRegion.GetRegionByParentRegionAndCompanyId(int.Parse(sheng), int.Parse(base.Administrator.CompanyId));
                this.ddlShi.DataTextField = "regionName";
                this.ddlShi.DataValueField = "id";
                ddlShi.DataSource = dtCity;
                ddlShi.DataBind();
            }

            ddlShi.Items.Insert(0, new ExtAspNet.ListItem("选择地区市", "-1"));
        }

        private void BindArea()
        {
            string shi = ddlShi.SelectedValue;
            this.ddlXian.Items.Clear();
            if (shi != "-1")
            {
                DataTable dtArea = BaseFactory.theRegion.GetRegionByParentRegionAndCompanyId(int.Parse(shi), int.Parse(base.Administrator.CompanyId));
                this.ddlXian.DataTextField = "regionName";
                this.ddlXian.DataValueField = "id";
                ddlXian.DataSource = dtArea;
                ddlXian.DataBind();
            }
            if (ddlXian.Items.Count <= 0)
            {
                ddlXian.Items.Insert(0, new ExtAspNet.ListItem("选择地区市", ""));
            }
            else
            {
                ddlXian.Items.Insert(0, new ExtAspNet.ListItem("选择地区市", "-1"));
            }
        }

        protected void ddlSheng_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCity();
        }

        protected void ddlShi_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindArea();
           
        }

        protected void chk1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chk1.Checked)
            {
                this.txtLinker.Text = this.txtManager.Text;
                this.txtMobile.Text = this.txtManagerMobile.Text;
            }
  
        }

      
 
       
 
    }
}